package org.lib.jpamysql.query;

import java.util.Date;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.NamedStoredProcedureQuery;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureParameter;
import javax.persistence.Transient;

import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.lib.jpamysql.query.meiju.UserSex;

import com.vladmihalcea.hibernate.type.json.JsonStringType;

import lombok.Data;

/**
 * 用户
 * @author ben
 * @date 2021-10-25 14:57:13 CST
 */
@Entity
@Data
@TypeDef(name = "json", typeClass = JsonStringType.class)
// 使用@NamedQueries
@NamedQueries(value= {
		// 根据firstName查找
		@NamedQuery(name="User.findInNamedQuery", query="SELECT u FROM User u WHERE u.firstName = ?1")
})
// 使用存储过程
@NamedStoredProcedureQuery(
		name = "User.plus1",
		procedureName = "plus1inout",
		parameters = {
				@StoredProcedureParameter(mode=ParameterMode.IN, name="arg", type=Integer.class),
				@StoredProcedureParameter(mode=ParameterMode.OUT, name="res", type=Integer.class),
		}
)
public class User {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	
	@Column(columnDefinition = "VARCHAR(100) NOT NULL")
	private String firstName;
	
	@Column(columnDefinition = "VARCHAR(100) NOT NULL")
	private String lastName;

	@Column(columnDefinition = "INT DEFAULT 0")
	private Integer age;
	
	@Column(columnDefinition = "VARCHAR(255) NOT NULL")
	private String email;
	
	@Column(columnDefinition = "VARCHAR(100) NOT NULL")
	private String phone;
	
	// `sex` int(11) DEFAULT NULL
	private UserSex sex;
	
	private Integer property;
	
	private Float height;
	
	private Float weight;
	
	// 211030试验：默认类型 varchar(100)
	private String slogan;
	
	// 不存库
	@Transient
	private String abcNotInDb;
	
	/**
	 * 方法查询策略的属性表达式
	 */
	// 错误方式
//	@Column(columnDefinition = "VARCHAR(200)")
	// 正确方式
	@Embedded
	@AttributeOverrides({
			@AttributeOverride(name="zipCode", column = @Column(name = "address_zip_code")),
			@AttributeOverride(name="province", column = @Column(name = "address_province")),
			@AttributeOverride(name="city", column = @Column(name = "address_city")),
			@AttributeOverride(name="district", column = @Column(name = "address_district"))
	})
	private Address address;
	
//	private Address address2;
	
	// 无用: Address被@Embeddable，不能设置为 JSON类型
//	@Type(type = "json")
//	@Column(columnDefinition = "json")
//	private Address jsonAddress;
	
	// 可以设置为 JSON类型
	@Type(type = "json")
	@Column(columnDefinition = "json")
	private Address2 jsonAddress2;
	
	@Column(insertable = false, columnDefinition = "DATETIME DEFAULT NOW()")
	private Date createTime;
	
	@Column(insertable = false, updatable = false, columnDefinition = "DATETIME DEFAULT NOW() ON UPDATE NOW()")
	private Date updateTime;
	
}
